package sufutian.com.google_speech;

import android.content.Context;
import android.util.Log;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import network.MyJson;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by sufutian on 2017/6/28.
 */

public class SpeechBuiness implements Callback {

    private String TAG = "sufutian==";

    Context context;

    OkHttpClient client = new OkHttpClient().newBuilder()
            .connectTimeout(10, TimeUnit.SECONDS)//设置超时时间
            .readTimeout(10, TimeUnit.SECONDS)//设置读取超时时间
            .writeTimeout(10, TimeUnit.SECONDS)//设置写入超时时间
            .build();

    public SpeechBuiness(Context context) {
        this.context = context;
    }

    public void getResult(String name, String apikey) {

        String requestUrl = "https://speech.googleapis.com/v1/operations/" + name + "?" + "key=" + apikey;

        Request.Builder requestBuilder = new Request.Builder()
                .url(requestUrl)
                .get();

        try {
            client.newCall(requestBuilder.build()).enqueue(this);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onFailure(Call call, IOException e) {

        Log.e("sufutian==", "onFailure: ");

    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        Log.e(TAG, "onResponse: ");
        ResponseBody body = response.body();
        MyJson json = null;
        if (body != null) {
            String string = body.string();
            json = new MyJson(string);
            Log.e(TAG, "response.body: " + string);
        }

        if (json != null) { //有 json
            if (json.has("done")) {
                boolean ifDone = json.getBoolean("done");
                while (!ifDone) {//未完成
                    if (json.has("name")) {
                        String name = json.getString("name");
                        //延迟请求
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.e(TAG, ": 再一次网络请求");
                        getResult(name, "AIzaSyBqRkKbmeZ_7NIMSQWciT7-Y4bK1kwjnWQ");

                    }
                }
                if (ifDone) {//完成
                    if (json.has("response")) {
                        MyJson jsonResponse = json.getJson("response");
                        Log.e(TAG, "jsonResponse: " + jsonResponse.toString());
                    }
//                    LongRunningRecognizeResponse.parseFrom()

                }
            } else {//没有done 字段
                if (json.has("name")) {
                    String name = json.getString("name");
                    //延迟请求
                    Log.e(TAG, ": 再一次网络请求");
                    getResult(name, "AIzaSyBqRkKbmeZ_7NIMSQWciT7-Y4bK1kwjnWQ");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }


    }
}
